package swift.core.magic
{
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	
	import org.ais.event.TEvent;
	import org.aisy.interfaces.IClear;
	
	use namespace magic_internal;

	public class MagicVM
	{
		public function MagicVM()
		{
		}
		
		/**
		 * 
		 * 编译时转换基本数据类型
		 * 
		 * @param v
		 * @param i
		 * @param l
		 * 
		 */
		static magic_internal function compileCastObj(v:Array, i:uint, l:uint):void
		{
			var key:String, obj:*;
			for (; i < l; i++) {
				key = v[i] as String;
				switch (key) {
					case "true":
						v[i] = true;
						break;
					case "false":
						v[i] = false;
						break;
					case "null":
						v[i] = null;
						break;
					case "undefined":
						v[i] = undefined;
						break;
					case "NaN":
						v[i] = NaN;
						break;
					default:
						if (/^[\+|\-]?\d+$/g.test(key) === true) {
							obj = parseInt(key);
							if (obj > int.MAX_VALUE) {
								v[i] = uint(obj);
							}
							else {
								v[i] = int(obj);
							}
						}
						else if (/^[\+\-]?\d*\.?\d+(e|e\+\d+)?$/g.test(key) === true) {
							v[i] = parseFloat(key);
						}
						else if (/^[\+\-]?0?[xX][\da-fA-F]+$/.test(key) === true) {
							switch (key.charAt(0)) {
								case "+":
									obj = parseInt(key.charAt(1) !== "0" ? ("0" + key.substr(1)) : key);
									break;
								case "-":
									obj = parseInt(key.charAt(1) !== "0" ? ("-0" + key.substr(1)) : key);
									break;
								default:
									obj = parseInt(key.charAt(0) !== "0" ? ("0" + key) : key);
									break;
							}
							if (obj > int.MAX_VALUE) {
								v[i] = uint(obj);
							}
							else {
								v[i] = int(obj);
							}
						}
						break;
				}
			}
			key = null;
			obj = null;
			v = null;
		}
		
		/**
		 * 
		 * 删除对象
		 * 
		 * @param systemData
		 * @param currentVarData
		 * @param values
		 * 
		 */
		static magic_internal function delObj(systemData:Dictionary, currentVarData:MagicVarData, values:Array):void
		{
			var i:uint = 1, j:uint, l:uint = values.length, n:uint, key:String, v:Vector.<String>, obj:*;
//			判断操作符
			switch (values[0]) {
//				删除对象
				case "!":
//					遍历操作符之后的数据
					for (; i < l; i++) {
						key = values[i];
						switch (key.charAt(0)) {
//							根据第一个字符判断变量为自定义变量（只有自定义变量才可删除）
							case "$":
//								判断是否是删除对象的属性
								v = Vector.<String>(key.substr(1).split("."));
								n = v.length - 1;
//								删除的是对象本身
								if (n === 0) {
									key = getObj(systemData, currentVarData, v[0]);
//									返回对象所在的变量集合
									obj = currentVarData.getVars(key, true);
								}
								else {
									obj = currentVarData.getVars(getObj(systemData, currentVarData, v[0]), true);
									if (null !== obj) {
//										遍历最后一个属性之前的对象
										for (j = 1; j < n; j++) {
											obj = obj[getObj(systemData, currentVarData, v[j])];
										}
										key = getObj(systemData, currentVarData, v[j]);
									}
								}
								if (null !== obj) {
									delete obj[key];
								}
								break;
						}
					}
					break;
//				删除并销毁对象
				case "!!":
					var o:Object;
					for (; i < l; i++) {
						key = values[i];
						switch (key.charAt(0)) {
							case "$":
								v = Vector.<String>(key.substr(1).split("."));
								n = v.length - 1;
								if (n === 0) {
									key = getObj(systemData, currentVarData, v[0]);
									obj = currentVarData.getVars(key, true);
								}
								else {
									obj = currentVarData.getVars(getObj(systemData, currentVarData, v[0]), true);
									if (null !== obj) {
										for (j = 1; j < n; j++) {
											obj = obj[getObj(systemData, currentVarData, v[j])];
										}
										key = getObj(systemData, currentVarData, v[j]);
									}
								}
								if (null !== obj) {
									o = obj[key];
									if (o is IClear) IClear(o).clear();
									delete obj[key];
									o = null;
								}
								break;
						}
					}
					o = null;
					break;
			}
			key = null;
			v = null;
			obj = null;
			systemData = null;
			currentVarData = null;
			values = null;
		}
		
		/**
		 * 
		 * 将名为 key 的对象的值设为 value（对象可能存在于 systemData 或 currentVarData）
		 * 
		 * @param systemData
		 * @param currentVarData
		 * @param key
		 * @param value
		 * 
		 */
		static magic_internal function setObj(systemData:Dictionary, currentVarData:MagicVarData, key:String, value:*):void
		{
			var i:uint = 1, l:uint, v:Vector.<String> = Vector.<String>(key.substr(1).split(".")), obj:*;
			l = v.length - 1;
			switch (key.charAt(0)) {
//				系统对象
				case "@":
					if (null !== systemData) {
						if (l === 0) {
							systemData[getObj(systemData, currentVarData, v[0])] = value;
						}
//						设置对象的属性
						else {
							obj = systemData[getObj(systemData, currentVarData, v[0])];
							for (; i < l; i++) {
								obj = obj[getObj(systemData, currentVarData, v[i])];
							}
							obj[getObj(systemData, currentVarData, v[i])] = value;
						}
					}
					break;
//				自定义对象
				case "$":
					if (l === 0) {
						if (null === currentVarData.vars) {
							currentVarData.vars = new Dictionary();
						}
						currentVarData.vars[getObj(systemData, currentVarData, v[0])] = value;
					}
					else {
						obj = currentVarData.vars[getObj(systemData, currentVarData, v[0])];
						for (; i < l; i++) {
							obj = obj[getObj(systemData, currentVarData, v[i])];
						}
						obj[getObj(systemData, currentVarData, v[i])] = value;
					}
					break;
			}
			v = null;
			obj = null;
			systemData = null;
			currentVarData = null;
			key = null;
			value = null;
		}
		
		/**
		 * 
		 * 获取对象的属性（可以禁用 stage，parent，dispatchEvent 等来提高安全性）
		 * 
		 * @param systemData
		 * @param currentVarData
		 * @param obj
		 * @param pros
		 * @return 
		 * 
		 */
		static magic_internal function getPro(systemData:Dictionary, currentVarData:MagicVarData, obj:Object, v:Vector.<String>):*
		{
			for (var i:uint, l:uint = v.length; i < l; i++) {
				obj = obj[getObj(systemData, currentVarData, v[i])];
			}
			systemData = null;
			currentVarData = null;
			v = null;
			return obj;
		}
		
		/**
		 * 
		 * 获取对象（获取一个变量、变量的属性、函数、定义的引用、创建一个对象，可以附带参数）
		 * 
		 * @param systemData
		 * @param currentVarData
		 * @param key
		 * @param parameters
		 * @return 
		 * 
		 */
		static magic_internal function getObj(systemData:Dictionary, currentVarData:MagicVarData, key:*, ...parameters):*
		{
			if ((key is String) === false) {
				systemData = null;
				currentVarData = null;
				parameters = null;
				return key;
			}
			var i:int, k:String = key, obj:* = null;
//			判断操作符
			switch (k.charAt(0)) {
//				系统对象
				case "@":
					if (null !== systemData) {
						i = k.indexOf(".");
						obj = k.substring(1, i === -1 ? k.length : i);
						if (systemData.hasOwnProperty(obj) === true) {
							obj = systemData[obj];
//							返回对象的属性
							if (i !== -1) {
								obj = getPro(systemData, currentVarData, obj, Vector.<String>(k.substr(i + 1).split(".")));
							}
						}
						else {
							obj = null;
						}
					}
					break;
//				自定义对象
				case "$":
					if (null !== currentVarData.vars) {
						i = k.indexOf(".");
						obj = currentVarData.getVar(k.substring(1, i === -1 ? k.length : i), true);
						if (i !== -1) {
							obj = getPro(systemData, currentVarData, obj, Vector.<String>(k.substr(i + 1).split(".")));
						}
					}
					break;
//				自定义函数
				case "^":
					obj = currentVarData.getFun(getObj(systemData, currentVarData, k.substr(1)), true);
					if (null !== obj) {
						obj = MagicFunctionUtil.create(exec, obj as MagicFunctionData, systemData, currentVarData);
					}
					break;
//				引用系统的定义（可以禁用或过滤某些定义来提高安全性）
				case "~":
					k = k.substr(1);
					i = k.indexOf(":");
					obj = getDefinitionByName(getObj(systemData, currentVarData, i === -1 ? k : k.substring(0, i)));
					if (i !== -1) {
						obj = getPro(systemData, currentVarData, obj, Vector.<String>(k.substr(i + 1).split(".")));
					}
					break;
//				创建一个系统定义的对象（可以禁用或过滤某些定义来提高安全性）
				case "#":
					var l:uint = parameters.length;
					for (i = 0; i < l; i++) {
						parameters[i] = getObj(systemData, currentVarData, parameters[i]);
					}
					obj = MagicClassUtil.create(getObj(systemData, currentVarData, k.substr(1)), parameters);
					break;
//				返回常量
				default:
					switch (k) {
						case "true":
							obj = true;
							break;
						case "false":
							obj = false;
							break;
						case "null":
							obj = null;
							break;
						case "undefined":
							obj = undefined;
							break;
						case "NaN":
							obj = NaN;
							break;
						default:
							if (/^[\+|\-]?\d+$/g.test(k) === true) {
								obj = parseInt(k);
								if (obj > int.MAX_VALUE) {
									obj = uint(obj);
								}
								else {
									obj = int(obj);
								}
							}
							else if (/^[\+\-]?\d*\.?\d+(e|e\+\d+)?$/g.test(k) === true) {
								obj = parseFloat(k);
							}
							else if (/^[\+\-]?0?[xX][\da-fA-F]+$/.test(k) === true) {
								switch (k.charAt(0)) {
									case "+":
										obj = parseInt(k.charAt(1) !== "0" ? ("0" + k.substr(1)) : k);
										break;
									case "-":
										obj = parseInt(k.charAt(1) !== "0" ? ("-0" + k.substr(1)) : k);
										break;
									default:
										obj = parseInt(k.charAt(0) !== "0" ? ("0" + k) : k);
										break;
								}
								if (obj > int.MAX_VALUE) {
									obj = uint(obj);
								}
								else {
									obj = int(obj);
								}
							}
							else {
								obj = k.replace(/^[\"\']|[\"\']$/g, "");
							}
							break;
					}
					break;
			}
			k = null;
			systemData = null;
			currentVarData = null;
			key = null;
			parameters = null;
			return obj;
		}
		
		/**
		 * 
		 * 赋值类操作符的运算（包括类型转换操作符）
		 * 
		 * @param systemData
		 * @param currentVarData
		 * @param v
		 * 
		 */
		static magic_internal function setOp(systemData:Dictionary, currentVarData:MagicVarData, v:Array):void
		{
			var i:uint = 2, l:uint = v.length, o:* = getObj(systemData, currentVarData, v[1]);
			switch (v[0]) {
				case "+":
					for (; i < l; i++) {
						o += getObj(systemData, currentVarData, v[i]);
					}
					setObj(systemData, currentVarData, v[1], o);
					break;
				case "-":
					for (; i < l; i++) {
						o -= getObj(systemData, currentVarData, v[i]);
					}
					setObj(systemData, currentVarData, v[1], o);
					break;
				case "*":
					for (; i < l; i++) {
						o *= getObj(systemData, currentVarData, v[i]);
					}
					setObj(systemData, currentVarData, v[1], o);
					break;
				case "/":
					for (; i < l; i++) {
						o /= getObj(systemData, currentVarData, v[i]);
					}
					setObj(systemData, currentVarData, v[1], o);
					break;
				case "%":
					for (; i < l; i++) {
						o %= getObj(systemData, currentVarData, v[i]);
					}
					setObj(systemData, currentVarData, v[1], o);
					break;
				case "++":
					setObj(systemData, currentVarData, v[1], o + 1);
					for (; i < l; i++) {
						setObj(systemData, currentVarData, v[i], getObj(systemData, currentVarData, v[i]) + 1);
					}
					break;
				case "--":
					setObj(systemData, currentVarData, v[1], o - 1);
					for (; i < l; i++) {
						setObj(systemData, currentVarData, v[i], getObj(systemData, currentVarData, v[i]) - 1);
					}
					break;
				case "^":
					for (; i < l; i++) {
						o ^= getObj(systemData, currentVarData, v[i]);
					}
					setObj(systemData, currentVarData, v[1], o);
					break;
				case "~":
					if (l > 2) {
						o = doOp(systemData, currentVarData, v.slice(2));
					}
					setObj(systemData, currentVarData, v[1], ~o);
					break;
				case "|":
					for (; i < l; i++) {
						o |= getObj(systemData, currentVarData, v[i]);
					}
					setObj(systemData, currentVarData, v[1], o);
					break;
				case "&":
					for (; i < l; i++) {
						o &= getObj(systemData, currentVarData, v[i]);
					}
					setObj(systemData, currentVarData, v[1], o);
					break;
				case "<<":
					for (; i < l; i++) {
						o <<= getObj(systemData, currentVarData, v[i]);
					}
					setObj(systemData, currentVarData, v[1], o);
					break;
				case ">>":
					for (; i < l; i++) {
						o >>= getObj(systemData, currentVarData, v[i]);
					}
					setObj(systemData, currentVarData, v[1], o);
					break;
				case ">>>":
					for (; i < l; i++) {
						o = o >>> getObj(systemData, currentVarData, v[i]);
					}
					setObj(systemData, currentVarData, v[1], o);
					break;
				case "||":
					for (; i < l; i++) {
						o ||= getObj(systemData, currentVarData, v[i]);
					}
					setObj(systemData, currentVarData, v[1], o);
					break;
				case "&&":
					for (; i < l; i++) {
						o &&= getObj(systemData, currentVarData, v[i]);
					}
					setObj(systemData, currentVarData, v[1], o);
					break;
				case "as":
					setObj(systemData, currentVarData, v[1], o as doOp(systemData, currentVarData, v.slice(2)));
					break;
//				强制类型转换
				case "cast":
					setObj(systemData, currentVarData, v[1], doOp(systemData, currentVarData, v.slice(2))(o));
					break;
//				typeof 操作符
				case "tf":
					if (l > 2) {
						o = doOp(systemData, currentVarData, v.slice(2));
					}
					setObj(systemData, currentVarData, v[1], typeof(o));
					break;
			}
			o = null;
			systemData = null;
			currentVarData = null;
			v = null;
		}
		
		/**
		 * 
		 * 执行对应操作符的操作
		 * 
		 * @param systemData
		 * @param currentVarData
		 * @param v
		 * @return 
		 * 
		 */
		static magic_internal function doOp(systemData:Dictionary, currentVarData:MagicVarData, v:Array):*
		{
			if (v.length === 0) return void;
			var o:* = void;
			switch (v[0]) {
//				声明并对对象赋值或对已有的对象赋值
				case "=":
					setObj(systemData, currentVarData, v[1], doOp(systemData, currentVarData, v.slice(2)));
					break;
//				赋值操作
				case "+":
				case "-":
				case "*":
				case "/":
				case "%":
				case "++":
				case "--":
				case "^":
				case "~":
				case "|":
				case "&":
				case "<<":
				case ">>":
				case ">>>":
				case "||":
				case "&&":
				case "as":
				case "cast":
				case "tf":
					setOp(systemData, currentVarData, v);
					break;
//				删除对象
				case "!":
				case "!!":
					delObj(systemData, currentVarData, v);
					break;
//				返回对象的 key 的数组
				case "fi":
					o = doOp(systemData, currentVarData, v.slice(2));
					if (null !== o) {
						var i:uint, arr:Array = [], k:*;
						for (k in o) {
							arr[i++] = k;
						}
						setObj(systemData, currentVarData, v[1], arr);
						arr = null;
						k = null;
						o = null;
					}
					break;
//				返回对象的 value 的数组
				case "fe":
					o = doOp(systemData, currentVarData, v.slice(2));
					if (null !== o) {
						i = 0;
						arr = [];
						for each (k in o) {
							arr[i++] = k;
						}
						setObj(systemData, currentVarData, v[1], arr);
						arr = null;
						k = null;
						o = null;
					}
					break;
//				返回对象的 [key, value] 的二维数组
				case "fei":
					o = doOp(systemData, currentVarData, v.slice(2));
					if (null !== o) {
						i = 0;
						arr = [];
						for (k in o) {
							arr[i++] = [k, o[k]];
						}
						setObj(systemData, currentVarData, v[1], arr);
						arr = null;
						k = null;
						o = null;
					}
					break;
//				print 操作符，具体的输出流需自定义实现
				case "p":
					TEvent.trigger("MAGICVM", "p", doOp(systemData, currentVarData, v.slice(1)));
					break;
//				抛出异常
				case "throw":
					o = doOp(systemData, currentVarData, v.slice(1));
					throw (o is Error ? o : Error(o));
					break;
				default:
					if (v[0] is String) {
//						判断操作符
						switch ((v[0] as String).charAt(0)) {
//							执行函数操作符
							case ":":
								i = 1;
								arr = [];
								for (var j:uint, l:uint = v.length; i < l; i++, j++) {
									arr[j] = getObj(systemData, currentVarData, v[i]);
								}
								o = getObj(systemData, currentVarData, (v[0] as String).substr(1));
								if (o is Function) {
									o = (o as Function).apply(null, arr);
								}
								else {
									o = void;
								}
								arr = null;
								break;
//							获取一个对象
							default:
								o = getObj.apply(null, [systemData, currentVarData].concat(v));
								break;
						}
					}
					else if (v.length === 1) {
						o = v[0];
					}
					else {
						o = getObj.apply(null, [systemData, currentVarData].concat(v));
					}
					break;
			}
			systemData = null;
			currentVarData = null;
			v = null;
			return o;
		}
		
		/**
		 * 
		 * 逻辑操作符的操作
		 * 
		 * @param systemData
		 * @param currentVarData
		 * @param v
		 * @return 
		 * 
		 */
		static magic_internal function doIf(systemData:Dictionary, currentVarData:MagicVarData, v:Array):Boolean
		{
			var b:Boolean, o:* = getObj(systemData, currentVarData, v[2]);
			switch (v[1]) {
				case "=":
					if (v.length === 3) {
						if (o) {
							b = true;
						}
					}
					else {
						b = true;
						for (var i:uint = 3, l:uint = v.length; i < l; i++) {
							if (o != getObj(systemData, currentVarData, v[i])) {
								b = false;
								break;
							}
						}
					}
					break;
				case "!":
					if (v.length === 3) {
						if (!o) {
							b = true;
						}
					}
					else {
						b = true;o
						for (i = 3, l = v.length; i < l; i++) {
							if (o == getObj(systemData, currentVarData, v[i])) {
								b = false;
								break;
							}
						}
					}
					break;
				case "<":
					b = true;
					var o2:*;
					for (i = 3, l = v.length; i < l; i++) {
						o2 = getObj(systemData, currentVarData, v[i]);
						if (o >= o2) {
							b = false;
							break;
						}
						else {
							o = o2;
						}
					}
					o2 = null;
					break;
				case ">":
					b = true;
					for (i = 3, l = v.length; i < l; i++) {
						o2 = getObj(systemData, currentVarData, v[i]);
						if (o <= o2) {
							b = false;
							break;
						}
						else {
							o = o2;
						}
					}
					o2 = null;
					break;
				case "<=":
					b = true;
					for (i = 3, l = v.length; i < l; i++) {
						o2 = getObj(systemData, currentVarData, v[i]);
						if (o > o2) {
							b = false;
							break;
						}
						else {
							o = o2;
						}
					}
					o2 = null;
					break;
				case ">=":
					b = true;
					for (i = 3, l = v.length; i < l; i++) {
						o2 = getObj(systemData, currentVarData, v[i]);
						if (o < o2) {
							b = false;
							break;
						}
						else {
							o = o2;
						}
					}
					o2 = null;
					break;
				case "is":
					b = true;
					for (i = 3, l = v.length; i < l; i++) {
						if (!(o is getObj(systemData, currentVarData, v[i]))) {
							b = false;
							break;
						}
					}
					break;
			}
			o = null;
			systemData = null;
			currentVarData = null;
			v = null;
			return b;
		}
		
		/**
		 * 
		 * 执行一个函数语句块
		 * 
		 * @param currentFB 将要执行的函数语句块
		 * @param systemData 系统对象集合
		 * @param parentVarData 父级变量数据
		 * @param callee
		 * @param parameters 函数的实际参数
		 * @return 
		 * 
		 */
		static public function exec(currentFB:MagicFunctionData, systemData:Dictionary = null, parentVarData:MagicVarData = null, callee:Function = null, ...parameters):*
		{
//			如果系统对象集合为空，则创建一个
			if (null === systemData) systemData = new Dictionary();
			parameters["callee"] = callee;
			callee = null;
//			this，arguments 属于系统对象
			systemData["this"] = currentFB;
			systemData["arguments"] = parameters;
			var fromIndex:uint, bIndex:uint, invalidVLen:uint, invalidV:Vector.<uint> = currentFB.invalidV, cmds:Vector.<Array> = currentFB.cmds, v:Array, bk:MagicBlockData = currentFB, bV:Vector.<MagicBlockData>, wV:Vector.<MagicBlockData>, tV:Vector.<MagicBlockData>, tE:Error, sw:*, currentVarData:MagicVarData = new MagicVarData();
//			设置变量数据的域
			currentVarData.fun = currentFB;
//			设置变量数据的父级
			if (null !== parentVarData) {
				currentVarData.parent = parentVarData;
				parentVarData = null;
			}
//			将函数的实际参数根据形式参数复制到变量数据中
			bIndex = parameters.length;
			if (bIndex !== 0) {
				currentVarData.vars = new Dictionary();
				for (var j:uint; j < bIndex; j++) {
					currentVarData.vars[currentFB.parameters[j]] = parameters[j];
				}
			}
//			判断是否存在无效语句，bIndex 为需要跳过的语句索引
			if (null !== invalidV) {
				invalidVLen = invalidV.length;
				bIndex = invalidV[0];
			}
			else {
				bIndex = uint.MAX_VALUE;
			}
			
			parameters = null;
			
//			对语句块执行复位，bk 为当前语句块
			bk.blockIndex = 0;
			bk.nextIndex = 0;
			bk.flag = true;
			
			var i:uint = currentFB.start, l:uint = currentFB.end !== 0 ? currentFB.end : cmds.length;
			
//			执行语句
			while (i < l) {
				if (null === tV) {
					for (; i < l;) {
//						判断当前语句是否是无效语句，如果是则跳到下一有效语句
						if (i === bIndex) {
							i = invalidV[fromIndex + 1] + 1;
							fromIndex += 2;
							if (fromIndex === invalidVLen) {
								bIndex = uint.MAX_VALUE;
							}
							else {
								bIndex = invalidV[fromIndex];
							}
							continue;
						}
						else if (i > bIndex) {
							for (fromIndex = 0; fromIndex < invalidVLen; fromIndex += 2) {
								if (i <= invalidV[fromIndex]) {
									bIndex = invalidV[fromIndex];
									break;
								}
							}
							if (i === bIndex) {
								i = invalidV[fromIndex + 1] + 1;
								fromIndex += 2;
								if (fromIndex === invalidVLen) {
									bIndex = uint.MAX_VALUE;
								}
								else {
									bIndex = invalidV[fromIndex];
								}
								continue;
							}
							else if (fromIndex === invalidVLen) {
								bIndex = uint.MAX_VALUE;
							}
							else {
								bIndex = invalidV[fromIndex];
							}
						}
						v = cmds[i];
//						判断操作符
						switch (v[0]) {
							case "if":
								if (null !== bk.blockV) {
//									跳到 if 的子语句块中，并追加索引
									bk = bk.blockV[bk.blockIndex++];
//									对语句块执行复位
									bk.blockIndex = 0;
									bk.nextIndex = 0;
									bk.flag = false;
//									判断语句逻辑结果
									if (doIf(systemData, currentVarData, v) === true) {
//										为 true 时则将语句块的执行标记设为 true，语句块内的语句可以执行
										bk.flag = true;
									}
//									如果结果为 false，判断是否有后续语句块（elseif，else），并进行跳转
									else if (null !== bk.nextV) {
										bk = bk.nextV[bk.nextIndex++];
										bk.blockIndex = 0;
										bk.nextIndex = 0;
										bk.flag = false;
										i = bk.start;
										continue;
									}
//									如果没有后续语句则跳出 if 语句块，到父级
									else {
										i = bk.end;
										bk = bk.parent;
										continue;
									}
								}
								break;
							case "elseif":
//								如果之前的标记为 true 则证明已经完成 if 判断，将跳出 if 语句块，到父级
								if (bk.flag === true) {
									bk.flag = false;
//									如果之前是 elseif 语句块，则将 bk 置为 if 语句块（if 是 elseif，else 语句块的父级）
									if (bk.type === MagicEnum.TYPE_ELSE) {
										bk = bk.parent;
									}
									i = bk.end;
									bk = bk.parent;
									continue;
								}
								else {
//									执行判断
									if (doIf(systemData, currentVarData, v) === true) {
										bk.flag = true;
									}
									else {
//										将 bk 设为 if 语句块
										bk = bk.parent;
//										是否有其他后续语句存在
										if (bk.nextIndex !== bk.nextV.length) {
											bk = bk.nextV[bk.nextIndex];
											bk.blockIndex = 0;
											bk.nextIndex = 0;
											bk.flag = false;
											i = bk.start;
											continue;
										}
										else {
											i = bk.end;
											bk = bk.parent;
											continue;
										}
									}
								}
								break;
							case "else":
//								参考 elseif
								if (bk.flag === true) {
									bk.flag = false;
									if (bk.type === MagicEnum.TYPE_ELSE) {
										bk = bk.parent;
									}
									i = bk.end;
									bk = bk.parent;
									continue;
								}
								else {
									bk.flag = true;
								}
								break;
							case "endif":
//								结束 if 语句块
//								如果之前是 elseif，else 语句块，则将 bk 置为 if 语句块（if 是 elseif，else 语句块的父级）
								if (bk.type === MagicEnum.TYPE_ELSE) {
									bk = bk.parent;
								}
//								跳出 if 语句块，到父级
								bk = bk.parent;
								break;
							case "switch":
								if (null !== bk.blockV) {
//									参考 if
									bk = bk.blockV[bk.blockIndex];
									bk.blockIndex = 0;
									bk.nextIndex = 0;
									bk.flag = false;
									if (null !== bk.nextV) {
//										将比较对象赋值给 sw（switch 语句块的临时比较变量）
										sw = doOp(systemData, currentVarData, v.slice(1));
//										由于 switch 结构存在嵌套，将存在嵌套的语句块加入 bV（存在嵌套的语句块的集合）
										if (null === bV) {
											bV = new Vector.<MagicBlockData>();
										}
										bV[bV.length] = bk;
										bk = bk.nextV[bk.nextIndex++];
										bk.blockIndex = 0;
										bk.nextIndex = 0;
										bk.flag = false;
										i = bk.start;
									}
//									不包含 case，default，则跳到父级
									else {
										i = bk.end;
										bk = bk.parent;
									}
									continue;
								}
								break;
							case "case":
//								如果没有匹配项则对 case 的结果进行比较
								if (bk.flag === false) {
									if (sw === doOp(systemData, currentVarData, v.slice(1))) {
										bk.flag = true;
										sw = null;
									}
									else {
//										参考 elseif
										bk = bk.parent;
										if (bk.nextIndex !== bk.nextV.length) {
											bk = bk.nextV[bk.nextIndex++];
											bk.blockIndex = 0;
											bk.nextIndex = 0;
											bk.flag = false;
											i = bk.start;
										}
										else {
											i = bk.end;
											bk = bk.parent;
//											移除 bV 的最后一项，即当前的 switch 语句块
											if (bV.length === 1) {
												bV = null;
											}
											else {
												bV.length--;
											}
										}
										continue;
									}
								}
								break;
							case "default":
//								参考 case 和 else
								if (bk.flag === false) {
									bk.flag = true;
									sw = null;
								}
								break;
							case "endswitch":
//								结束 switch 语句块
//								跳出语句块，到父级
								bk = bk.parent;
//								追加语句块索引
								bk.blockIndex++;
//								移除 bV 的最后一项，即当前的 switch 语句块
								if (bV.length === 1) {
									bV = null;
								}
								else {
									bV.length--;
								}
								break;
							case "while":
								if (null !== bk.blockV) {
//									参考 if
									bk = bk.blockV[bk.blockIndex];
									bk.blockIndex = 0;
									bk.nextIndex = 0;
									bk.flag = false;
									if (doIf(systemData, currentVarData, v) === true) {
										bk.flag = true;
//										参考 switch
										if (null === bV) {
											bV = new Vector.<MagicBlockData>();
										}
//										while 语句块的集合
										if (null === wV) {
											wV = new Vector.<MagicBlockData>();
										}
										bV[bV.length] = wV[wV.length] = bk;
//										对循环结构将 bIndex 重置
										if (bIndex >= bk.end) {
											bIndex = 0;
										}
									}
									else {
//										跳到当前语句块结束行
										i = bk.end;
//										跳出语句块，到父级
										bk = bk.parent;
//										追加语句块索引
										bk.blockIndex++;
										continue;
									}
								}
								break;
							case "endwhile":
//								结束 while 语句块
//								进行下次循环，跳到 while 语句块开始
								i = bk.start;
								bk = bk.parent;
//								参考 endswitch
								if (bV.length === 1) {
									bV = null;
									wV = null;
								}
								else {
									bV.length--;
									if (wV.length === 1) {
										wV = null;
									}
									else {
										wV.length--;
									}
								}
								continue;
								break;
							case "break":
//								结束最内层的 switch，while 语句块
//								将当前语句块 bk 置为当前最内层的 switch，while 语句块
								bk = bV[bV.length - 1];
//								参考 endwhile
								if (bV.length === 1) {
									bV = null;
									wV = null;
								}
								else {
									bV.length--;
									if (null !== wV) {
										if (bk === wV[wV.length - 1]) {
											if (wV.length === 1) {
												wV = null;
											}
											else {
												wV.length--;
											}
										}
									}
								}
//								跳到当前语句块结束行
								i = bk.end;
//								跳出语句块，到父级
								bk = bk.parent;
//								追加语句块索引
								bk.blockIndex++;
								continue;
								break;
							case "continue":
//								将当前语句块 bk 置为当前最内层的 while 语句块
								bk = wV[wV.length - 1];
//								将 bV 的最后一项置为当前最内层的 while 语句块
								while (bk !== bV[bV.length - 1]) {
									bV.length--;
								}
//								参考 break
								if (bV.length === 1) {
									bV = null;
									wV = null;
								}
								else {
									bV.length--;
									if (wV.length === 1) {
										wV = null;
									}
									else {
										wV.length--;
									}
								}
//								跳到当前语句块开始行
								i = bk.start;
//								跳出语句块，到父级
								bk = bk.parent;
								continue;
								break;
							case "return":
								if (bk.flag === true) {
									invalidV = null;
									cmds = null;
									bk = null;
									bV = null;
									wV = null;
									tV = null;
									tE = null;
									sw = null;
									currentFB = null;
									if (v.length === 1) return null;
//									返回语句的结果
									else return doOp(systemData, currentVarData, v.slice(1));
								}
								break;
							case "try":
								if (null !== bk.blockV) {
//									参考 if
									bk = bk.blockV[bk.blockIndex++];
									bk.blockIndex = 0;
									bk.nextIndex = 0;
									bk.flag = true;
//									try catch 语句块的集合
									if (null === tV) {
										tV = new Vector.<MagicBlockData>();
									}
									tV[tV.length] = bk;
								}
								break;
							case "catch":
//								没有异常
								if (bk.flag === true) {
//									跳出语句块，到父级
									i = bk.end;
									bk = bk.parent;
//									将 tV 的最后一项置为当前最内层的 try catch 语句块
									if (null !== tV) {
										if (tV.length === 1) {
											tV = null;
										}
										else {
											tV.length--;
										}
									}
								}
//								存在异常
								else {
//									标记可以执行
									bk.flag = true;
									if (v.length !== 1) {
//										将异常 tE 赋值给 catch 的形式参数
										if (null === currentVarData.vars) {
											currentVarData.vars = new Dictionary();
										}
										currentVarData.vars[(v[1] as String).charAt(0) === "$" ? (v[1] as String).substr(1) : v[1]] = tE;
									}
									tE = null;
								}
								break;
							case "endtrycatch":
//								结束 try catch 语句块
//								跳到 try 语句块的父级（try 是 catch 语句块的父级）
								bk = bk.parent.parent;
//								将 tV 的最后一项置为当前最内层的 try catch 语句块
								if (null !== tV) {
									if (tV.length === 1) {
										tV = null;
									}
									else {
										tV.length--;
									}
								}
								tE = null;
								break;
							default:
//								如果语句块可执行，则执行对应操作符的操作
								if (bk.flag === true) {
									doOp(systemData, currentVarData, v);
								}
								break;
						}
						i++;
//						如果 tV 不为 null，即存在 try catch 块，则跳出此循环至 try catch 循环模式
						if (null !== tV) break;
					}
				}
				else {
					try {
						for (; i < l;) {
//							判断当前语句是否是无效语句，如果是则跳到下一有效语句
							if (i === bIndex) {
								i = invalidV[fromIndex + 1] + 1;
								fromIndex += 2;
								if (fromIndex === invalidVLen) {
									bIndex = uint.MAX_VALUE;
								}
								else {
									bIndex = invalidV[fromIndex];
								}
								continue;
							}
							else if (i > bIndex) {
								for (fromIndex = 0; fromIndex < invalidVLen; fromIndex += 2) {
									if (i <= invalidV[fromIndex]) {
										bIndex = invalidV[fromIndex];
										break;
									}
								}
								if (i === bIndex) {
									i = invalidV[fromIndex + 1] + 1;
									fromIndex += 2;
									if (fromIndex === invalidVLen) {
										bIndex = uint.MAX_VALUE;
									}
									else {
										bIndex = invalidV[fromIndex];
									}
									continue;
								}
								else if (fromIndex === invalidVLen) {
									bIndex = uint.MAX_VALUE;
								}
								else {
									bIndex = invalidV[fromIndex];
								}
							}
							v = cmds[i];
//							判断操作符
							switch (v[0]) {
								case "if":
									if (null !== bk.blockV) {
//										跳到 if 的子语句块中，并追加索引
										bk = bk.blockV[bk.blockIndex++];
//										对语句块执行复位
										bk.blockIndex = 0;
										bk.nextIndex = 0;
										bk.flag = false;
//										判断语句逻辑结果
										if (doIf(systemData, currentVarData, v) === true) {
//											为 true 时则将语句块的执行标记设为 true，语句块内的语句可以执行
											bk.flag = true;
										}
//										如果结果为 false，判断是否有后续语句块（elseif，else），并进行跳转
										else if (null !== bk.nextV) {
											bk = bk.nextV[bk.nextIndex++];
											bk.blockIndex = 0;
											bk.nextIndex = 0;
											bk.flag = false;
											i = bk.start;
											continue;
										}
//										如果没有后续语句则跳出 if 语句块，到父级
										else {
											i = bk.end;
											bk = bk.parent;
											continue;
										}
									}
									break;
								case "elseif":
//									如果之前的标记为 true 则证明已经完成 if 判断，将跳出 if 语句块，到父级
									if (bk.flag === true) {
										bk.flag = false;
//										如果之前是 elseif 语句块，则将 bk 置为 if 语句块（if 是 elseif，else 语句块的父级）
										if (bk.type === MagicEnum.TYPE_ELSE) {
											bk = bk.parent;
										}
										i = bk.end;
										bk = bk.parent;
										continue;
									}
									else {
//										执行判断
										if (doIf(systemData, currentVarData, v) === true) {
											bk.flag = true;
										}
										else {
//											将 bk 设为 if 语句块
											bk = bk.parent;
//											是否有其他后续语句存在
											if (bk.nextIndex !== bk.nextV.length) {
												bk = bk.nextV[bk.nextIndex];
												bk.blockIndex = 0;
												bk.nextIndex = 0;
												bk.flag = false;
												i = bk.start;
												continue;
											}
											else {
												i = bk.end;
												bk = bk.parent;
												continue;
											}
										}
									}
									break;
								case "else":
//									参考 elseif
									if (bk.flag === true) {
										bk.flag = false;
										if (bk.type === MagicEnum.TYPE_ELSE) {
											bk = bk.parent;
										}
										i = bk.end;
										bk = bk.parent;
										continue;
									}
									else {
										bk.flag = true;
									}
									break;
								case "endif":
//									结束 if 语句块
//									如果之前是 elseif，else 语句块，则将 bk 置为 if 语句块（if 是 elseif，else 语句块的父级）
									if (bk.type === MagicEnum.TYPE_ELSE) {
										bk = bk.parent;
									}
//									跳出 if 语句块，到父级
									bk = bk.parent;
									break;
								case "switch":
									if (null !== bk.blockV) {
//										参考 if
										bk = bk.blockV[bk.blockIndex];
										bk.blockIndex = 0;
										bk.nextIndex = 0;
										bk.flag = false;
										if (null !== bk.nextV) {
//											将比较对象赋值给 sw（switch 语句块的临时比较变量）
											sw = doOp(systemData, currentVarData, v.slice(1));
//											由于 switch 结构存在嵌套，将存在嵌套的语句块加入 bV（存在嵌套的语句块的集合）
											if (null === bV) {
												bV = new Vector.<MagicBlockData>();
											}
											bV[bV.length] = bk;
											bk = bk.nextV[bk.nextIndex++];
											bk.blockIndex = 0;
											bk.nextIndex = 0;
											bk.flag = false;
											i = bk.start;
										}
//										不包含 case，default，则跳到父级
										else {
											i = bk.end;
											bk = bk.parent;
										}
										continue;
									}
									break;
								case "case":
//									如果没有匹配项则对 case 的结果进行比较
									if (bk.flag === false) {
										if (sw === doOp(systemData, currentVarData, v.slice(1))) {
											bk.flag = true;
											sw = null;
										}
										else {
//											参考 elseif
											bk = bk.parent;
											if (bk.nextIndex !== bk.nextV.length) {
												bk = bk.nextV[bk.nextIndex++];
												bk.blockIndex = 0;
												bk.nextIndex = 0;
												bk.flag = false;
												i = bk.start;
											}
											else {
												i = bk.end;
												bk = bk.parent;
//												移除 bV 的最后一项，即当前的 switch 语句块
												if (bV.length === 1) {
													bV = null;
												}
												else {
													bV.length--;
												}
											}
											continue;
										}
									}
									break;
								case "default":
//									参考 case 和 else
									if (bk.flag === false) {
										bk.flag = true;
										sw = null;
									}
									break;
								case "endswitch":
//									结束 switch 语句块
//									跳出语句块，到父级
									bk = bk.parent;
//									追加语句块索引
									bk.blockIndex++;
//									移除 bV 的最后一项，即当前的 switch 语句块
									if (bV.length === 1) {
										bV = null;
									}
									else {
										bV.length--;
									}
									break;
								case "while":
									if (null !== bk.blockV) {
//										参考 if
										bk = bk.blockV[bk.blockIndex];
										bk.blockIndex = 0;
										bk.nextIndex = 0;
										bk.flag = false;
										if (doIf(systemData, currentVarData, v) === true) {
											bk.flag = true;
//											参考 switch
											if (null === bV) {
												bV = new Vector.<MagicBlockData>();
											}
//											while 语句块的集合
											if (null === wV) {
												wV = new Vector.<MagicBlockData>();
											}
											bV[bV.length] = wV[wV.length] = bk;
//											对循环结构将 bIndex 重置
											if (bIndex >= bk.end) {
												bIndex = 0;
											}
										}
										else {
//											跳到当前语句块结束行
											i = bk.end;
//											跳出语句块，到父级
											bk = bk.parent;
//											追加语句块索引
											bk.blockIndex++;
											continue;
										}
									}
									break;
								case "endwhile":
//									结束 while 语句块
//									进行下次循环，跳到 while 语句块开始
									i = bk.start;
									bk = bk.parent;
//									参考 endswitch
									if (bV.length === 1) {
										bV = null;
										wV = null;
									}
									else {
										bV.length--;
										if (wV.length === 1) {
											wV = null;
										}
										else {
											wV.length--;
										}
									}
									continue;
									break;
								case "break":
//									结束最内层的 switch，while 语句块
//									将当前语句块 bk 置为当前最内层的 switch，while 语句块
									bk = bV[bV.length - 1];
//									参考 endwhile
									if (bV.length === 1) {
										bV = null;
										wV = null;
									}
									else {
										bV.length--;
										if (null !== wV) {
											if (bk === wV[wV.length - 1]) {
												if (wV.length === 1) {
													wV = null;
												}
												else {
													wV.length--;
												}
											}
										}
									}
//									跳到当前语句块结束行
									i = bk.end;
//									跳出语句块，到父级
									bk = bk.parent;
//									追加语句块索引
									bk.blockIndex++;
									continue;
									break;
								case "continue":
//									将当前语句块 bk 置为当前最内层的 while 语句块
									bk = wV[wV.length - 1];
//									将 bV 的最后一项置为当前最内层的 while 语句块
									while (bk !== bV[bV.length - 1]) {
										bV.length--;
									}
//									参考 break
									if (bV.length === 1) {
										bV = null;
										wV = null;
									}
									else {
										bV.length--;
										if (wV.length === 1) {
											wV = null;
										}
										else {
											wV.length--;
										}
									}
//									跳到当前语句块开始行
									i = bk.start;
//									跳出语句块，到父级
									bk = bk.parent;
									continue;
									break;
								case "return":
									if (bk.flag === true) {
										invalidV = null;
										cmds = null;
										bk = null;
										bV = null;
										wV = null;
										tV = null;
										tE = null;
										sw = null;
										currentFB = null;
										if (v.length === 1) return null;
//										返回语句的结果
										else return doOp(systemData, currentVarData, v.slice(1));
									}
									break;
								case "try":
									if (null !== bk.blockV) {
//										参考 if
										bk = bk.blockV[bk.blockIndex++];
										bk.blockIndex = 0;
										bk.nextIndex = 0;
										bk.flag = true;
//										try catch 语句块的集合
										if (null === tV) {
											tV = new Vector.<MagicBlockData>();
										}
										tV[tV.length] = bk;
									}
									break;
								case "catch":
//									没有异常
									if (bk.flag === true) {
//										跳出语句块，到父级
										i = bk.end;
										bk = bk.parent;
//										将 tV 的最后一项置为当前最内层的 try catch 语句块
										if (null !== tV) {
											if (tV.length === 1) {
												tV = null;
											}
											else {
												tV.length--;
											}
										}
									}
//									存在异常
									else {
//										标记可以执行
										bk.flag = true;
										if (v.length !== 1) {
//											将异常 tE 赋值给 catch 的形式参数
											if (null === currentVarData.vars) {
												currentVarData.vars = new Dictionary();
											}
											currentVarData.vars[(v[1] as String).charAt(0) === "$" ? (v[1] as String).substr(1) : v[1]] = tE;
										}
										tE = null;
									}
									break;
								case "endtrycatch":
//									结束 try catch 语句块
//									跳到 try 语句块的父级（try 是 catch 语句块的父级）
									bk = bk.parent.parent;
//									将 tV 的最后一项置为当前最内层的 try catch 语句块
									if (null !== tV) {
										if (tV.length === 1) {
											tV = null;
										}
										else {
											tV.length--;
										}
									}
									tE = null;
									break;
								default:
//									如果语句块可执行，则执行对应操作符的操作
									if (bk.flag === true) {
										doOp(systemData, currentVarData, v);
									}
									break;
							}
							i++;
//							如果 tV 为 null，即不存在 try catch 块，则跳出此循环至无 try catch 循环模式
							if (null === tV) break;
						}
					}
					catch (error:Error) {
//						捕获异常，为安全考虑，暂不列出详细信息
						tE = new Error("line: " + (i + 1) + " :: " + error.message, error.errorID);
//						将当前语句块 bk 置为当前最内层的 try 语句块
						bk = tV[tV.length - 1];
//						跳至 catch 语句块
						bk = bk.nextV[bk.nextIndex];
						bk.blockIndex = 0;
						bk.nextIndex = 0;
//						标记存在异常
						bk.flag = false;
//						跳到 catch 语句块开始行
						i = bk.start;
//						将 tV 的最后一项置为当前最内层的 try catch 语句块
						if (tV.length === 1) {
							tV = null;
						}
						else {
							tV.length--;
						}
						continue;
					}
				}
			}
			invalidV = null;
			cmds = null;
			v = null;
			bk = null;
			bV = null;
			wV = null;
			tV = null;
			tE = null;
			sw = null;
			currentVarData = null;
			currentFB = null;
			systemData = null;
			return void;
		}
		
		/**
		 * 
		 * 编译语句（返回可执行数据或编译错误信息 ）
		 * 
		 * @param cmd <String or Array>
		 * @param parentBlockData
		 * @return 
		 * 
		 */
		static public function compile(cmd:*, parentBlockData:MagicFunctionData = null):Object
		{
			if (null === parentBlockData) parentBlockData = new MagicFunctionData();
			if (null === parentBlockData.cmds) parentBlockData.cmds = new Vector.<Array>();
			var str:String, cmds:Array, o:Object, r:RegExp = /[^\s\"\']+|\".*?\"|\'.*?\'/g, v:Array, currentFB:MagicFunctionData = parentBlockData, bk:MagicBlockData = currentFB, bk2:MagicBlockData, fV:Vector.<MagicFunctionData> = Vector.<MagicFunctionData>([currentFB]), error:String;
//			将语句分析成行
			if (cmd is String) {
				cmds = (cmd as String).split(/\r\n|\r|\n/);
			}
			else if (cmd is Array) {
				cmds = cmd;
			}
			cmd = null;
			for (var i:uint, j:uint = parentBlockData.cmds.length, l:uint = cmds.length, n:uint; i < l;) {
				str = cmds[i];
				v = [];
				r.lastIndex = 0;
//				每行数据长度
				n = 0;
//				分析出每行的数据，放到数组 v 中
				while (null !== (o = r.exec(str))) {
					v[n++] = o[0];
				}
				parentBlockData.cmds[j++] = v;
//				空行，将行号添加到无效语句行集合中
				if (n === 0) {
					if (null === currentFB.invalidV) {
						currentFB.invalidV = new Vector.<uint>();
					}
//					将连续的无效语句行更新到无效语句行集合中
					if (currentFB.invalidV.length !== 0 && currentFB.invalidV[currentFB.invalidV.length - 1] + 1 === i) {
						currentFB.invalidV[currentFB.invalidV.length - 1] = i;
					}
					else {
//						将开始行号，和结束行号添加到无效语句行集合中
						currentFB.invalidV[currentFB.invalidV.length] = i;
						currentFB.invalidV[currentFB.invalidV.length] = i;
					}
					i++;
					continue;
				}
//				根据操作符，将语句分块
				switch (v[0]) {
					case "function":
//						没有函数名
						if (n === 1) {
							error = "the function has no name, at line: " + (i + 1);
							parentBlockData.clear();
							str = null;
							cmds = null;
							o = null;
							r = null;
							v = null;
							currentFB = null;
							bk = null;
							bk2 = null;
							fV = null;
							parentBlockData = null;
							return error;
						}
//						新的函数语句块
						currentFB = new MagicFunctionData();
//						记录父级
						currentFB.parent = fV[fV.length - 1];
						currentFB.cmds = parentBlockData.cmds;
//						可执行语句的开始行
						currentFB.start = i + 1;
//						记录函数的形式参数
						if (n !== 2) {
							currentFB.parameters = new Vector.<String>();
							var k:String;
							for (var e:uint = 0, m:uint = 2; m < n; e++, m++) {
								k = v[m];
								currentFB.parameters[e] = k.charAt(0) === "$" ? k.substr(1) : k;
							}
							k = null;
						}
//						将当前函数语句块添加到函数块集合中
						fV[fV.length] = currentFB;
//						将当前语句块 bk 置为函数语句块
						bk = currentFB;
//						将此函数添加到父级的内部函数集合中
						currentFB = currentFB.parent as MagicFunctionData;
						if (null === currentFB.fun) {
							currentFB.fun = new Dictionary();
						}
						currentFB.fun[v[1]] = bk;
//						函数定义行被认为是无效行（不是可以执行的）
						if (null === currentFB.invalidV) {
							currentFB.invalidV = new Vector.<uint>();
						}
						if (currentFB.invalidV.length > 1 && currentFB.invalidV[currentFB.invalidV.length - 1] + 1 === i) {
							currentFB.invalidV.length--;
						}
						else {
							currentFB.invalidV[currentFB.invalidV.length] = i;
						}
						currentFB = fV[fV.length - 1];
						break;
					case "endfunction":
//						结束函数语句块
						currentFB.end = i;
//						移除 fV 的最后一项
						fV.length--;
//						将当前函数语句块置为前一个
						currentFB = fV[fV.length - 1];
//						函数结束操作符是无效行
						currentFB.invalidV[currentFB.invalidV.length] = i;
//						将当前语句块 bk 置为函数语句块
						bk = currentFB;
						break;
					case "if":
					case "switch":
					case "while":
					case "try":
						switch (v[0]) {
							case "if":
							case "while":
								if (n < 3) {
									error = "incorrect number of arguments, at line: " + (i + 1);
									parentBlockData.clear();
									str = null;
									cmds = null;
									o = null;
									r = null;
									v = null;
									currentFB = null;
									bk = null;
									bk2 = null;
									fV = null;
									parentBlockData = null;
									return error;
								}
								else {
									switch (v[1]) {
										case "=":
										case "!":
										case "<":
										case ">":
										case "<=":
										case ">=":
										case "is":
											break;
										default:
											error = "operator is wrong, at line: " + (i + 1);
											parentBlockData.clear();
											str = null;
											cmds = null;
											o = null;
											r = null;
											v = null;
											currentFB = null;
											bk = null;
											bk2 = null;
											fV = null;
											parentBlockData = null;
											return error;
											break;
									}
								}
//								在编译时转换基本数据类型常量（如果可能的话，int boolean 等）
								compileCastObj(v, 2, n);
								break;
							case "switch":
								if (n === 1) {
									error = "incorrect number of arguments, at line: " + (i + 1);
									parentBlockData.clear();
									str = null;
									cmds = null;
									o = null;
									r = null;
									v = null;
									currentFB = null;
									bk = null;
									bk2 = null;
									fV = null;
									parentBlockData = null;
									return error;
								}
								compileCastObj(v, 1, n);
								break;
						}
//						将生成新的子语句块
						if (null === bk.blockV) {
							bk.blockV = new Vector.<MagicBlockData>();
						}
						bk2 = new MagicBlockData();
						bk2.start = i;
						bk2.parent = bk;
						bk.blockV[bk.blockV.length] = bk2;
						bk = bk2;
						break;
					case "else":
					case "elseif":
					case "case":
					case "default":
					case "catch":
						switch (v[0]) {
							case "elseif":
								if (n < 3) {
									error = "incorrect number of arguments, at line: " + (i + 1);
									parentBlockData.clear();
									str = null;
									cmds = null;
									o = null;
									r = null;
									v = null;
									currentFB = null;
									bk = null;
									bk2 = null;
									fV = null;
									parentBlockData = null;
									return error;
								}
								else {
									switch (v[1]) {
										case "=":
										case "!":
										case "<":
										case ">":
										case "<=":
										case ">=":
										case "is":
											break;
										default:
											error = "operator is wrong, at line: " + (i + 1);
											parentBlockData.clear();
											str = null;
											cmds = null;
											o = null;
											r = null;
											v = null;
											currentFB = null;
											bk = null;
											bk2 = null;
											fV = null;
											parentBlockData = null;
											return error;
											break;
									}
								}
								compileCastObj(v, 2, n);
								break;
							case "case":
								if (n === 1) {
									error = "incorrect number of arguments, at line: " + (i + 1);
									parentBlockData.clear();
									str = null;
									cmds = null;
									o = null;
									r = null;
									v = null;
									currentFB = null;
									bk = null;
									bk2 = null;
									fV = null;
									parentBlockData = null;
									return error;
								}
								compileCastObj(v, 1, n);
								break;
						}
//						参考 if
						if (null === bk2.nextV) {
							bk2.nextV = new Vector.<MagicBlockData>();
						}
						bk = new MagicBlockData();
						bk.type = MagicEnum.TYPE_ELSE;
						bk.start = i;
						bk.parent = bk2;
						bk2.nextV[bk2.nextV.length] = bk;
						break;
					case "endif":
					case "endswitch":
					case "endwhile":
					case "endtrycatch":
//						结束当前语句块，跳到父级
						bk2.end = i + 1;
						bk2 = bk2.parent;
						if (bk2.type === MagicEnum.TYPE_ELSE) {
							bk2 = bk2.parent;
						}
						bk = bk2;
						break;
					case "break":
					case "continue":
					case "++":
					case "--":
					case "!":
					case "!!":
					case "as":
					case "cast":
						switch (v[0]) {
							case "++":
							case "--":
							case "!":
							case "!!":
								if (n === 1) {
									error = "incorrect number of arguments, at line: " + (i + 1);
									parentBlockData.clear();
									str = null;
									cmds = null;
									o = null;
									r = null;
									v = null;
									currentFB = null;
									bk = null;
									bk2 = null;
									fV = null;
									parentBlockData = null;
									return error;
								}
								else {
//									操作数必须为变量等数据对象
									for (m = 1; m < n; m++) {
										switch ((v[m] as String).charAt(0)) {
											case "@":
											case "$":
												break;
											default:
												error = "the operand must be a variable, an element in an array, or a property of an object, at line: " + (i + 1);
												parentBlockData.clear();
												str = null;
												cmds = null;
												o = null;
												r = null;
												v = null;
												currentFB = null;
												bk = null;
												bk2 = null;
												fV = null;
												parentBlockData = null;
												return error;
												break;
										}
									}
								}
								break;
						}
						break;
					case "return":
						if (n !== 1) compileCastObj(v, 1, n);
						break;
					case "throw":
					case "p":
						if (n === 1) {
							error = "incorrect number of arguments, at line: " + (i + 1);
							parentBlockData.clear();
							str = null;
							cmds = null;
							o = null;
							r = null;
							v = null;
							currentFB = null;
							bk = null;
							bk2 = null;
							fV = null;
							parentBlockData = null;
							return error;
						}
						compileCastObj(v, 1, n);
						break;
					case "=":
					case "+":
					case "-":
					case "*":
					case "/":
					case "%":
					case "^":
					case "~":
					case "|":
					case "&":
					case "<<":
					case ">>":
					case ">>>":
					case "||":
					case "&&":
					case "tf":
						switch (v[0]) {
							case "~":
							case "tf":
								if (n === 1) {
									error = "incorrect number of arguments, at line: " + (i + 1);
									parentBlockData.clear();
									str = null;
									cmds = null;
									o = null;
									r = null;
									v = null;
									currentFB = null;
									bk = null;
									bk2 = null;
									fV = null;
									parentBlockData = null;
									return error;
								}
								break;
							default:
								if (n < 3) {
									error = "incorrect number of arguments, at line: " + (i + 1);
									parentBlockData.clear();
									str = null;
									cmds = null;
									o = null;
									r = null;
									v = null;
									currentFB = null;
									bk = null;
									bk2 = null;
									fV = null;
									parentBlockData = null;
									return error;
									break;
								}
						}
						switch ((v[1] as String).charAt(0)) {
							case "@":
							case "$":
								break;
							default:
								error = "the operand must be a variable, an element in an array, or a property of an object, at line: " + (i + 1);
								parentBlockData.clear();
								str = null;
								cmds = null;
								o = null;
								r = null;
								v = null;
								currentFB = null;
								bk = null;
								bk2 = null;
								fV = null;
								parentBlockData = null;
								return error;
								break;
						}
						compileCastObj(v, 2, n);
						break;
					case "fi":
					case "fe":
					case "fei":
						if (n < 3) {
							error = "incorrect number of arguments, at line: " + (i + 1);
							parentBlockData.clear();
							str = null;
							cmds = null;
							o = null;
							r = null;
							v = null;
							currentFB = null;
							bk = null;
							bk2 = null;
							fV = null;
							parentBlockData = null;
							return error;
						}
						switch ((v[1] as String).charAt(0)) {
							case "@":
							case "$":
								break;
							default:
								error = "the operand must be a variable, an element in an array, or a property of an object, at line: " + (i + 1);
								parentBlockData.clear();
								str = null;
								cmds = null;
								o = null;
								r = null;
								v = null;
								currentFB = null;
								bk = null;
								bk2 = null;
								fV = null;
								parentBlockData = null;
								return error;
								break;
						}
						if (n !== 3) compileCastObj(v, 3, n);
						break;
					default:
						switch ((v[0] as String).charAt(0)) {
							case "#":
							case ":":
								break;
							default:
//								没有匹配到的操作符或操作标记的语句都是无效语句
								if (null === currentFB.invalidV) {
									currentFB.invalidV = new Vector.<uint>();
								}
								if (currentFB.invalidV.length !== 0 && currentFB.invalidV[currentFB.invalidV.length - 1] + 1 === i) {
									currentFB.invalidV[currentFB.invalidV.length - 1] = i;
								}
								else {
									currentFB.invalidV[currentFB.invalidV.length] = i;
									currentFB.invalidV[currentFB.invalidV.length] = i;
								}
								break;
						}
						break;
				}
				i++;
			}
//			验证语句块是否结束
			if (bk !== parentBlockData) {
				if (fV.length !== 1) {
					bk2 = fV[fV.length - 1];
					if (bk === bk2) {
						bk = null;
					}
					null === error ? (error = "") : (error += "\n");
					error += "the function has not end, at line: " + bk2.start;
				}
				if (null !== bk) {
					null === error ? (error = "") : (error += "\n");
					error += "the block has not end, at line: " + (bk.start + 1);
				}
				parentBlockData.clear();
				parentBlockData = null;
			}
			
			str = null;
			cmds = null;
			o = null;
			r = null;
			v = null;
			currentFB = null;
			bk = null;
			bk2 = null;
			fV = null;
			return null !== error ? error : parentBlockData;
		}
		
	}
}

internal namespace magic_internal = "213384665b731cdf2fe17d13266786f65ceee0e1ab799e0ee704860761556606";